<?php

namespace App\Http\Controllers\Wxapi\newWork;

use App\Library\WxNotify;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class IndexController extends CommonController
{
    private $settingTableName = "new_work_setting";
    private $catTableName = "new_work_cat";
    private $typeTableName = "new_work_type";
    private $formTableName = "setting_form";
    private $workListTableName = "new_work_list";
    private $workChangeTableName = "new_work_change";
    public function __construct()
    {
        parent::__construct(["wxNotify", "getSetting", "getClassList"]);
    }

    public function wxNotify(){
        $notify = new WxNotify();
        $result = $notify->handleNotify();
        return response()->json($result);
    }

    // 获取工单设置信息
    public function getSetting(){
        if ($this->result['status'] > 0){
            return $this->return_result($this->result);
        }
        $settingData = DB::table($this->settingTableName)->where("mall_id", request()->mallData["id"])->first();
        $settingData = json_decode(json_encode($settingData),true);
        if(!$settingData) {
            $this->result['status'] = 1;
            $this->result['msg'] = '工单未设置';
            return $this->return_result($this->result);
        }
        $this->result['data'] = $this->transferFields($settingData);
        $this->result['data']["banner"] = $this->processingPictures($this->result["data"]["banner"]);
        return $this->return_result($this->result);
    }

    // 获取工单服务分类列表
    public function getClassList(){
        if ($this->result['status'] > 0){
            return $this->return_result($this->result);
        }
        $settingData = DB::table($this->settingTableName)->where("mall_id", request()->mallData["id"])->first();
        $settingData = json_decode(json_encode($settingData),true);
        if(!$settingData) {
            $this->result['status'] = 1;
            $this->result['msg'] = '工单未设置';
            return $this->return_result($this->result);
        }
        $settingData = $this->transferFields($settingData);
        $settingData["banner"] = $this->processingPictures($settingData["banner"]);
        $catData = DB::table($this->catTableName)->select("id", "name", "remark", "icon")
            ->where("status", 1)->orderBy("sort", "desc")->where("mall_id", request()->mallData["id"])->get();
        $catData = $this->transferFields(json_decode(json_encode($catData),true));
        foreach ($catData as &$item){
            $item["icon"] = $this->processingPictures($item["icon"]);
        }
        $this->result['data'] = ["serverList" => $catData, "setting" => $settingData];
        return $this->return_result($this->result);
    }

    // 获取工单填写表单信息
    public function getWorkOrderInfo(){
        if ($this->result['status'] > 0){
            return $this->return_result($this->result);
        }
        $param = request()->post();
        if(empty($param["catId"])){
            $this->result['status'] = 1;
            $this->result['msg'] = '服务分类ID不能为空';
            return $this->return_result($this->result);
        }
        // 服务分类信息
        $catData = DB::table($this->catTableName)->select("picture", "intro", "form_id")
            ->where("id", $param["catId"])->first();
        $catData = $this->transferFields(json_decode(json_encode($catData),true));
        if(!empty($catData)) {
            $catData["picture"] = $this->processingPictures($catData["picture"]);
            // 服务类型
            $typeData = DB::table($this->typeTableName)->select("id", "name", "class")->where("cat_id", $param["catId"])->where("status", 1)->get();
            $typeData = (json_decode(json_encode($typeData),true));
            foreach ($typeData as &$item){
                $item["class"] = @json_decode($item["class"], true);
            }
            // 表单信息 form_data
            $formData = DB::table($this->formTableName)->select("form_data")->where("id", $catData["formId"])->first();
            $formData = $this->transferFields(json_decode(json_encode($formData),true));
            $this->result['data'] = [
                "listData" => $catData, "typeData" => $typeData,
                "formData" => !empty($formData["formData"]) ? @json_decode($formData["formData"], true) : []
            ];
        }
        return $this->return_result($this->result);
    }

    // 保存工单表单信息
    public function toSaveWorkOrder(){
        if ($this->result['status'] > 0){
            return $this->return_result($this->result);
        }
        $param = request()->post();
        $field = ["cat_id" => "require", "type_id" => "require", "feedback" => "require", "tag" => "require"];//"title" => "require"
        $fieldMsg = ["cat_id" => "服务分类ID", "type_id" => "服务类型ID", "feedback" => "反馈内容", "tag" => "标签", "title" => "反馈标题"];
        $data = ["user_id" => $this->user["id"], "create_time" => date("Y-m-d H:i:s"), "status" => 1];
        foreach ($field as $k => $v) {
            $ks = Str::camel($k); //转换字段，变驼峰
            if($v === 'require' && !isset($param[$ks])){
                $this->result['status'] = 1;
                $this->result['msg'] = "{$fieldMsg[$k]}不能为空";
                return $this->return_result($this->result);
            }
            $data[$k] = isset($param[$ks]) ? trim($param[$ks]) : $v;
        }
        // 服务类型
        $typeData = DB::table($this->typeTableName)->select("server_id", "is_receive")->where("id", $data["type_id"])->first();
        $typeData = @json_decode(json_encode($typeData),true);
        if(isset($typeData["is_receive"]) && $typeData["is_receive"] == "1"){ // 默认接单了
            $data["receive_id"] = $typeData["server_id"];
        }
        $data["mall_id"] = request()->mallData["id"];
        try{
            if($id = DB::table($this->workListTableName)->insertGetId($data)){
                if(!isset($data["receive_id"]) && !DB::table($this->workChangeTableName)->insert([
                    "list_id" => $id, "remark" => "系统分配", "status" => 1, "create_time" => date("Y-m-d H:i:s"), "mall_id" => request()->mallData["id"]
                    ])){ // 后台分配转单
                    $this->result['status'] = 1;
                    $this->result['msg'] = '资料保存成功，后台分配转单失败';
                    return $this->return_result($this->result);
                }
                $setting = DB::table($this->settingTableName)->where("mall_id", request()->mallData["id"])->select("sms_remind_order", "remind_mobile")->first();
                if(!empty($setting->remind_mobile) && $setting->sms_remind_order){ // 发送短信
                    $this->sendSms($setting->remind_mobile, $setting->sms_remind_order, []); // 发送短信
                }
            }else{
                $this->result['status'] = 1;
                $this->result['msg'] = '保存资料失败';
                return $this->return_result($this->result);
            }
        }catch (\Exception $e){
            $this->result['status'] = 1;
            $this->result['msg'] = $e->getMessage();
            return $this->return_result($this->result);
        }
        $this->result['msg'] = "资料保存成功";
        return $this->return_result($this->result);
    }
}
